home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Extras / Development / Example_Code_v37 / ARexx / simplerexx / SimpleRexxExample.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-03-17  |  4.8 KB  |  220 lines

  1. /*
  2.  * This is an example of how to use the SimpleRexx code.
  3.  */
  4.  
  5. #include    <exec/types.h>
  6. #include    <libraries/dos.h>
  7. #include    <libraries/dosextens.h>
  8. #include    <intuition/intuition.h>
  9.  
  10. #include    <clib/exec_protos.h>
  11. #include    <clib/intuition_protos.h>
  12.  
  13. #include    <rexx/storage.h>
  14. #include    <rexx/rxslib.h>
  15.  
  16. #include    <stdio.h>
  17. #include    <string.h>
  18.  
  19. #include    "SimpleRexx.h"
  20.  
  21. /*
  22.  * Lattice control-c stop...
  23.  */
  24. int CXBRK(void) { return(0); }  /* Disable Lattice CTRL/C handling */
  25. int chkabort(void) { return(0); }  /* really */
  26.  
  27. /*
  28.  * The strings in this program
  29.  */
  30. char *strings[]=
  31. {
  32.     "50: Window already open",        /* STR_ID_WINDOW_OPEN */
  33.     "101: Window did not open",        /* STR_ID_WINDOW_ERROR */
  34.     "50: No Window",            /* STR_ID_WINDOW_NONE */
  35.     "80: Argument error to WINDOW command",    /* STR_ID_WINDOW_ARG */
  36.     "100: Unknown command",            /* STR_ID_COMMAND_ERROR */
  37.     "ARexx port name: %s\n",        /* STR_ID_PORT_NAME */
  38.     "No ARexx on this system.\n",        /* STR_ID_NO_AREXX */
  39.     "SimpleRexxExample Window"        /* STR_ID_WINDOW_TITLE */
  40. };
  41.  
  42. #define    STR_ID_WINDOW_OPEN    0
  43. #define    STR_ID_WINDOW_ERROR    1
  44. #define    STR_ID_WINDOW_NONE    2
  45. #define    STR_ID_WINDOW_ARG    3
  46. #define    STR_ID_COMMAND_ERROR    4
  47. #define    STR_ID_PORT_NAME    5
  48. #define    STR_ID_NO_AREXX        6
  49. #define    STR_ID_WINDOW_TITLE    7
  50.  
  51. /*
  52.  * NewWindow structure...
  53.  */
  54. static struct NewWindow nw=
  55. {
  56.     97,47,299,44,(UBYTE)-1,(UBYTE)-1,
  57.     CLOSEWINDOW,
  58.     WINDOWSIZING|WINDOWDRAG|WINDOWDEPTH|WINDOWCLOSE|SIMPLE_REFRESH|NOCAREREFRESH,
  59.     NULL,NULL,
  60.     NULL,
  61.     NULL,NULL,290,40,(UWORD)-1,(UWORD)-1,WBENCHSCREEN
  62. };
  63.  
  64. struct IntuitionBase *IntuitionBase;
  65.  
  66. /*
  67.  * A *VERY* simple and simple-minded example of using the SimpleRexx.c code.
  68.  *
  69.  * This program, when run, will print out the name of the ARexx port it
  70.  * opens.  Use that port to tell it to SHOW the window.  You can also
  71.  * use the ARexx port to HIDE the window, to READTITLE the window's
  72.  * titlebar, and QUIT the program.  You can also quit the program by
  73.  * pressing the close gadget in the window while the window is up.
  74.  *
  75.  * Note: You will want to RUN this program or have another shell available such
  76.  *       that you can still have access to ARexx...
  77.  */
  78. void main(int argc,char *argv[])
  79. {
  80. short    loopflag=TRUE;
  81. AREXXCONTEXT    RexxStuff;
  82. struct    Window    *win=NULL;
  83. ULONG    signals;
  84.  
  85.     if (IntuitionBase=(struct IntuitionBase *)
  86.                     OpenLibrary("intuition.library",NULL))
  87.     {
  88.         /*
  89.          * Note that SimpleRexx is set up such that you do not
  90.          * need to check for an error to initialize your REXX port
  91.          * This is so your application could run without REXX...
  92.          */
  93.         RexxStuff=InitARexx("Example","test");
  94.  
  95.         if (argc)
  96.         {
  97.             if (RexxStuff) printf(strings[STR_ID_PORT_NAME],
  98.                             ARexxName(RexxStuff));
  99.             else printf(strings[STR_ID_NO_AREXX]);
  100.         }
  101.  
  102.         while (loopflag)
  103.         {
  104.             signals=ARexxSignal(RexxStuff);
  105.             if (win) signals|=(1L << (win->UserPort->mp_SigBit));
  106.  
  107.             if (signals)
  108.             {
  109.             struct    RexxMsg        *rmsg;
  110.             struct    IntuiMessage    *msg;
  111.  
  112.                 signals=Wait(signals);
  113.  
  114.                 /*
  115.                  * Process the ARexx messages...
  116.                  */
  117.                 while (rmsg=GetARexxMsg(RexxStuff))
  118.                 {
  119.                 char    cBuf[24];
  120.                 char    *nextchar;
  121.                 char    *error=NULL;
  122.                 char    *result=NULL;
  123.                 long    errlevel=0;
  124.  
  125.                     nextchar=stptok(ARG0(rmsg),
  126.                                 cBuf,24," ,");
  127.                     if (*nextchar) nextchar++;
  128.  
  129.                     if (!stricmp("WINDOW",cBuf))
  130.                     {
  131.                         if (!stricmp("OPEN",nextchar))
  132.                         {
  133.                             if (win)
  134.                             {
  135.                                 error=strings[STR_ID_WINDOW_OPEN];
  136.                                 errlevel=5;
  137.                             }
  138.                             else
  139.                             {
  140.                                 nw.Title=strings[STR_ID_WINDOW_TITLE];
  141.                                 if (!(win=OpenWindow(&nw)))
  142.                                 {
  143.                                     error=strings[STR_ID_WINDOW_ERROR];
  144.                                     errlevel=30;
  145.                                 }
  146.                             }
  147.                         }
  148.                         else if (!stricmp("CLOSE",nextchar))
  149.                         {
  150.                             if (win)
  151.                             {
  152.                                 CloseWindow(win);
  153.                                 win=NULL;
  154.                             }
  155.                             else
  156.                             {
  157.                                 error=strings[STR_ID_WINDOW_NONE];
  158.                                 errlevel=5;
  159.                             }
  160.                         }
  161.                         else
  162.                         {
  163.                             error=strings[STR_ID_WINDOW_ARG];
  164.                             errlevel=20;
  165.                         }
  166.                     }
  167.                     else if (!stricmp("READTITLE",cBuf))
  168.                     {
  169.                         if (win)
  170.                         {
  171.                             result=win->Title;
  172.                         }
  173.                         else
  174.                         {
  175.                             error=strings[STR_ID_WINDOW_NONE];
  176.                             errlevel=5;
  177.                         }
  178.                     }
  179.                     else if (!stricmp("QUIT",cBuf))
  180.                     {
  181.                         loopflag=FALSE;
  182.                     }
  183.                     else
  184.                     {
  185.                         error=strings[STR_ID_COMMAND_ERROR];
  186.                         errlevel=20;
  187.                     }
  188.  
  189.                     if (error)
  190.                     {
  191.                         SetARexxLastError(RexxStuff,rmsg,error);
  192.                     }
  193.                     ReplyARexxMsg(RexxStuff,rmsg,result,errlevel);
  194.                 }
  195.  
  196.                 /*
  197.                  * If we have a window, process those messages
  198.                  */
  199.                 if (win) while (msg=(struct IntuiMessage *)
  200.                             GetMsg(win->UserPort))
  201.                 {
  202.                     if (msg->Class==CLOSEWINDOW)
  203.                     {
  204.                         /*
  205.                          * Quit if the close gadget...
  206.                          */
  207.                         loopflag=FALSE;
  208.                     }
  209.                     ReplyMsg((struct Message *)msg);
  210.                 }
  211.             }
  212.             else loopflag=FALSE;
  213.         }
  214.         if (win) CloseWindow(win);
  215.  
  216.         FreeARexx(RexxStuff);
  217.         CloseLibrary((struct Library *)IntuitionBase);
  218.     }
  219. }
  220.